//
//  ClassTabelView.m
//  MyItem
//
//  Created by SoulWater on 15/12/26.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "ClassTabelView.h"
#import "AFNetworking.h"
#import "wareModel.h"
#import "ClassCollectionView.h"
#import "ClassificationViewController.h"
@implementation ClassTabelView
{
    UITableViewCell *_cell;
    
    NSMutableArray *_nameArray;
    
    NSMutableArray *_modelArray;
    
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        [self createView];
        
    }
    return self;
}


-(void)createView
{
    self.dataSource = self;
    self.delegate = self;
    
    
    [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"classCell"];
    
    
    [self loadData];
    
}



#pragma mark - delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    _cell = [tableView dequeueReusableCellWithIdentifier:@"classCell" forIndexPath:indexPath];
    
    _cell.textLabel.text = _array[indexPath.row];
    _cell.textLabel.textAlignment = NSTextAlignmentCenter;
    _cell.textLabel.font = [UIFont systemFontOfSize:15];
    
    _cell.backgroundColor = [UIColor whiteColor];
    _cell.selectedTextColor = [UIColor redColor];
    _cell.selectedBackgroundView.backgroundColor = [UIColor lightGrayColor];

    return _cell;
}

//选中单元格
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //滑动到顶部
    [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //头视图图片
    [manager POST:_headUrlArray[indexPath.row] parameters:_headBodyArray[indexPath.row] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
//         NSLog(@"JSON: %@", responseObject);
        NSArray *cmsPromotionsList = responseObject[@"cmsPromotionsList"];
        NSDictionary *dic = cmsPromotionsList[0];
        
        ClassificationViewController *mainVC = (ClassificationViewController *)self.viewController;
        mainVC.collectionView.headViewUrl = dic[@"imageUrl"];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    //单元格
    [manager POST:_cellUrlArray[indexPath.row] parameters:_cellBodyArray[indexPath.row] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
//        NSLog(@"JSON: %@", responseObject);
        NSArray *data = responseObject[@"data"];

        _nameArray = [NSMutableArray array];
        _modelArray = [NSMutableArray array];
        for (NSDictionary *dataDic in data)
        {
            NSString *name = dataDic[@"name"];

            [_nameArray addObject:name];
            
            NSArray *catelogyList = dataDic[@"catelogyList"];
            
            for (NSDictionary *dic in catelogyList)
            {
                wareModel *model = [[wareModel alloc]initWithDataDic:dic];
                
                [_modelArray addObject:model];
            }
        }
        
        _dataArray = _modelArray;

        
        ClassificationViewController *mainVC = (ClassificationViewController *)self.viewController;
        mainVC.collectionView.dataArray = _dataArray;
        mainVC.collectionView.nameArray = _nameArray;

        [mainVC.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(void)loadData
{
    _array = @[@"推荐分类",@"鞋靴箱包",@"计生情趣",@"手机数码",@"图书",@"运动户外",@"音像制品",@"潮流女装",@"品牌男装",@"内衣配饰",@"家用电器",@"电脑办公",@"母婴频道",@"家居家纺",@"居家生活",@"家具建材",@"食品生鲜",@"个护化妆",@"户外运动"];
    _headUrlArray = @[
                      @"http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID",
                      @"http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID",
                      @"http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID",
                      @"http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID",
                      @"http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID",
                      @"http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID",
                      @"http://portal.m.jd.com/client.action?functionId=getCmsPromotionsListByCatelogyID"
                      ];
    _headBodyArray = @[
                       @{
                           @"sign": @"CQEXWEBKQ7TMDqSk_KAtUQ",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451196439830",
                           @"body": @"{\"level\":\"1\",\"catelogyID\":\"-1\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"UEZMryjPM-__IYtwyE8tNg",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451292797423",
                           @"body": @"{\"level\":\"1\",\"catelogyID\":\"1672\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                           },
                       @{
                           @"sign": @"xJgrKAB--feTLPOHL6OB-Q",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451292296435",
                           @"body": @"{\"level\":\"1\",\"catelogyID\":\"100001958\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"cxf85-GSv8eqedfgybxc8g",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451292632043",
                           @"body": @"{\"level\":\"1\",\"catelogyID\":\"9987\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"ZMpnETaQtpN-W3EONP7Jvg",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451292739480",
                           @"body": @"{\"level\":\"1\",\"catelogyID\":\"1713\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"YBB8IAnaCapawJgSe7x41w",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451292853541",
                           @"body": @"{\"level\":\"1\",\"catelogyID\":\"1318\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"uoZlffrwuAhCoT9iwQeUBw",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451293190761",
                           @"body": @"{\"level\":\"1\",\"catelogyID\":\"4051\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        }
                       
                       ];
    
    _cellUrlArray = @[
                      @"http://portal.m.jd.com/client.action?functionId=commonCatalogs",
                      @"http://portal.m.jd.com/client.action?functionId=newSubCatalog",
                      @"http://portal.m.jd.com/client.action?functionId=newSubCatalog",
                      @"http://portal.m.jd.com/client.action?functionId=newSubCatalog",
                      @"http://portal.m.jd.com/client.action?functionId=newSubCatalog",
                      @"http://portal.m.jd.com/client.action?functionId=newSubCatalog",
                      @"http://portal.m.jd.com/client.action?functionId=newSubCatalog"
                      ];
    
    _cellBodyArray = @[
                       @{
                           @"sign": @"Ew5GMcu8J3E9ZLqjs4IMRQ",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451110674264",
                           @"body": @"{\"method\":\"bp.category\",\"guid\":\"a1e609be3483012e09542a204a2a6dfd05c016be\",\"uid\":\"\",\"clear\":0,\"token\":\"f9715d6d28c7c4bb51ec5b757cb809f9\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"sJ0Nq3fXxl13YbGYFHXP6g",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451292213025",
                           @"body": @"{\"catelogyId\":\"1672\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"Y7Ks41JzlwI3Ueh_TpMcmg",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451292296431",
                           @"body": @"{\"catelogyId\":\"100001958\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"Y0sT7R7BOv_txhYbBCD4eQ",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451291293335",
                           @"body": @"{\"catelogyId\":\"9987\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"V1O6VkSY08jOxkwx99UvmA",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451291593193",
                           @"body": @"{\"catelogyId\":\"1713\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"8VpdJnQQZZH7RzRdETJWTw",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451292853536",
                           @"body": @"{\"catelogyId\":\"1318\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                        },
                       @{
                           @"sign": @"z8be92LZtD83BwA7HBnXBg",
                           @"d_model": @"iPhone4,1",
                           @"networkType": @"wifi",
                           @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                           @"st": @"1451293190757",
                           @"body": @"{\"catelogyId\":\"4051\"}",
                           @"osVersion": @"7.1.2",
                           @"d_brand": @"apple",
                           @"client": @"apple",
                           @"screen": @"640*960",
                           @"sv": @"1",
                           @"area": @"15_1213_3038_0",
                           @"build": @"35977",
                           @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                           @"clientVersion": @"4.4.3",
                           @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                           @"partner": @"apple"
                           }
                       ];
    
    
}

@end
